<?php
namespace app\home\controller;
use think\Controller;
class Ajax extends Controller{
    //构造函数
    public function _initialize(){
        parent::_initialize();
		$path = config('template.view_path', ROOT_PATH . 'tpl' . DS . 'user' . DS) . $GLOBALS['config']['site']['user_theme'] . DS;
        $this->view->config('view_path', $path);		
    }
	public function login(){
		$param = zanpiancms_param_url();
		return $this->fetch($param['type'] == 'pc' ? 'login/pcajax' : 'login/ajax');
	}
	public function reg() {
		$param = zanpiancms_param_url();
		return $this->fetch($param['type'] == 'pc' ? 'reg/pcajax' : 'reg/ajax');
	}		
   	public function get(){
		$param = zanpiancms_param_url();
		$info['gold']=$this->getgold($param);
	    $info['comment']=$this->getcm($param, $param['type']);
		return json($info);
	}
   	public function cm(){
		$param = zanpiancms_param_url();
		$info['comment']=$this->getcm($param, $param['type']);
        return json($info);		
	}	
   	private function getcm($param, $type = ''){
		if(!empty($param['id']) && !empty($param['sid'])){
			config('params',array('id'=>$param['id'],'sid'=>$param['sid'],'pid'=>0,'limit'=>$GLOBALS['config']['user']['sys']['user_cmnum'],'p'=>'[PAGE]'));
			config('route','home/ajax/cm');
			$this->assign('param',$param);
			return $this->fetch($type == 'pc' ? 'cm/pcall' : 'cm/all');
	    }	
	}
   	public function  gold(){
		$param = zanpiancms_param_url();
		$info['gold']=$this->getgold($param);
        return json($info);		
	}
   	private function getgold($param){
		$rs=model('gold');
		$model=$GLOBALS['config']['model'][$param['sid']];
		if(!empty($param['id']) && !empty($param['sid']) && !empty($model)){
			$start = array();
			$markdata = $rs->getGold($param['id'],$param['sid']);

			$value = $rs->getMarkValue($param['id'],$param['sid'],ip2long(request()->ip()));
			if($value > 0){
				$start['hadpingfen'] = 1 ;
				$start['mygold'] = $value ;
			}else{
				$start['mygold'] = 0 ;
			}
			$start['curpingfen'] = array();
			$gold=db($model['name'])->where($model['name'].'_id',$param['id'])->value($model['name'].'_gold');
			if($markdata != null){
				$start['curpingfen']['a'] = $markdata['F5'];
				$start['curpingfen']['b'] = $markdata['F4'];
				$start['curpingfen']['c'] = $markdata['F3'];
				$start['curpingfen']['d'] = $markdata['F2'];
				$start['curpingfen']['e'] = $markdata['F1'];
				$rate = $rs->getPingfen($markdata) ;
				if(count($rate) > 0){
					$start['curpingfen']['pinfenb'] = $rate['R2'];
					$start['curpingfen']['pinfen'] = $rate['R1'];
				}else{
					$start['curpingfen']['a'] = 0;
				    $start['curpingfen']['b'] = 0;
				    $start['curpingfen']['c'] = 0;
				    $start['curpingfen']['d'] = 0;
				    $start['curpingfen']['e'] = 0;
					$start['curpingfen']['pinfenb'] = $gold*10;
					$start['curpingfen']['pinfen'] = $gold;
				}
			}
		    $start['curpingfen']['num'] = $total;
			//是否收藏
			if(is_login() && $param['sid']==1){
				$user = session('user_auth');
				$favorite=db('favorite')->where(array('favorite_vid'=>$param['id'],'favorite_uid'=>$user['user_id']))->value('favorite_id');
				if($favorite){
					$start['loveid']=$param['id'];
				}
				$remin=db('remind')->where(array('remind_vid'=>$param['id'],'remind_uid'=>$user['user_id']))->value('remind_id');
				if($remin){
				    $start['remindid']=$param['id'];
				}
			}
            return $start;
        }

	}
	public function mark(){	
	    $param = zanpiancms_param_url();
	    $userinfo = session('user_auth');
	    if(empty($userinfo['user_id'])){
			return json(['msg'=>'请先登录','code' =>-1]);
		}
	    if ($this->request->instance()->isAjax()){
			if(!empty($param['id']) && !empty($param['cid']) && !empty($param['type'])){
				if($param['type']=='love'){
					$rs=model('favorite');
					$where=[];
					$where['favorite_vid']=$param['id'];
					$where['favorite_uid']=$userinfo['user_id'];
					$favorite=db('favorite')->where($where)->value('favorite_id');
			        if(!empty($favorite)){
				        if($rs->where($where)->delete()!== false){
				            return json(array('msg'=>'取消收藏成功','code' =>1));
				        }else{
							return json(array('msg'=>$rs->getError(),'code' =>-1));
						}
			        }else{
				        $data['favorite_vid'] = $param['id'];
				        $data['favorite_uid'] = $userinfo['user_id'];
				        $data['favorite_cid'] = $param['cid'];
				        $data['favorite_addtime'] = time();
				        if($rs->save($data)!== false){
					        return json(array('msg'=>"收藏成功",'code' =>1));
				        }else{
							return json(array('msg'=>$rs->getError(),'code' =>-1));
						}	
		            }
				}else{
                    $rs=model('remind');
                    $where=[];
					$where['remind_vid']=$param['id'];
					$where['remind_uid']=$userinfo['user_id'];
                    $remind=db('remind')->where($where)->value('remind_id');					
			        if(!empty($remind)){
				        if($rs->where($where)->delete()!== false){
				            return json(array('msg'=>'取消订阅成功','code' =>1));
				        }else{
							return json(array('msg'=>$rs->getError(),'code' =>-1));
						}
			        }else{
				        $vod_addtime=db('vod')->where('vod_id')->value('vod_addtime');
				        $data['remind_vid'] = $param['id'];
				        $data['remind_uid'] = $userinfo['user_id'];
				        $data['remind_cid'] = $param['cid'];
				        $data['remind_addtime'] = time();
				        $data['remind_uptime'] = $vod_addtime;
				        if($rs->save($data)!== false){
					        return json(array('msg'=>"订阅成功",'code' =>1));
				        }else{
							return json(array('msg'=>$rs->getError(),'code' =>-1));
						}
		            }						
				}	
			}else{
				return json(['msg'=>'参数错误','code' =>'-1']);
				
			}
		}	
	
	}
    //增加评分	
	public function addgold(){	
		$id = input('id/d',0);
		$sid = input('sid/d',1);
		$value = input('val/d',0);
		if(isset($id) && isset($value)){
			if($value == "1" || $value == "2" || $value == "3" || $value == "4" || $value == "5"){
				//查询一下
				$rs=model('gold');
				$count = $rs->where(array('gold_vid'=>$id,'gold_sid'=>$sid,'gold_ip'=>ip2long(request()->ip())))->count();
				if($count > 0){ //存在
				    return json(['msg'=>"已经评分,请务重复评分",'code'=>-1]);
				}else{ //增加评分
					$mark = array();
					$mark['gold_vid'] = $id;
					$mark['gold_sid'] = $sid;
					$mark['gold_ip'] = ip2long(request()->ip());
					$mark['gold_addtime'] = time();
					$mark['F'.$value] = 1 ;
					if($rs->insertGetId($mark)!== false){	
						$rate = $rs->getPingfen($id) ;
						if(count($rate) > 0){
                             db('vod')->where('vod_id',$id)->setField('vod_gold',$rate['R1']);
						}
						return json(['msg'=>"提交评分成功",'code'=>1]);
					}else{
						return json(['msg'=>$rs->getError(),'code'=>-1]);
					}
				}
			}
		}
	}	
	//评论顶踩	
	public function digg(){
	    $param = zanpiancms_param_url();			
		if(!empty($param['id']) && !empty($param['type'])){
		    $cookiename=md5('cm-'.ip2long(request()->ip()).'-'.intval($param['id']));
		    $cookie = cookie($cookiename);	
		    if($cookie){
				return json(['msg'=>'你已经点评过了！','code'=>-1]);
		    }			
			if(in_array($param['type'],array('up','down'))){
			    db('cm')->where('cm_id',$param['id'])->setInc('cm_'.$param['type']);
				cookie($cookiename,true,intval($GLOBALS['config']['user']['sys']['user_digg_time']));
				return json(['msg'=>'点评成功','code'=>1]);
			}
			return json(['msg'=>'参数错误','code'=>-1]);
		}else{
			return json(['msg'=>'参数错误','code'=>-1]);
		}
	}
    public function addcm(){
        $param = input();		
	    if($this->request->isPost()){
		    $userinfo = session('user_auth');
		    if(!$userinfo['user_id']){
		        return json(['msg'=>'请先登录','code' =>-2]); 		
		    }
			if($GLOBALS['config']['user']['sys']['user_code'] == 1){
                if(!captcha_check($param['validate'])){
					return json(['msg'=>'验证码错误','code' =>-1]);
                }
            }			
		    $rs=model('Cm');
		    $data=array();
		    $data['cm_content']=(string)htmlspecialchars(strip_tags(input('post.comm_txt/s'))); 
		    $data['cm_vid']=input('id/d');
		    $data['cm_uid']=$userinfo['user_id'];
		    $data['cm_username']=(string)$userinfo['user_nickname'];
		    $data['cm_sid']=input('sid/d',1);
		    $data['cm_pid']=input('post.comm_pid/d',0);
		    $data['cm_addtime']=time();
		    $data['checktime']=1;
		    if(empty($data['cm_content'])){
			    return json(['msg'=>'内容不能为空!','code' =>-1]);
		    }
		    $cookiename=md5('cm-'.bindec(decbin(ip2long(request()->ip()))).'-'.intval($data['cm_vid']).'-'.intval($data['cm_uid']));
		    $cookie=cookie($cookiename);
		    if($cookie){
			    return json(['msg'=>'亲您评论速度太快了,请休息一会，喝杯咖啡！','code' =>-1]); 
		    }			
		    if($rs->validate('cm')->save($data)!== false){
			    if($GLOBALS['config']['user']['sys']['user_cm_score']){
				    model('Score')->user_score($userinfo['user_id'],12, intval($GLOBALS['config']['user']['sys']['user_cm_score'])); 
			    }
			    cookie($cookiename,true,intval($GLOBALS['config']['user']['sys']['user_cm_time']));
			    if($GLOBALS['config']['user']['sys']['user_check']==1){
			        return json(['msg'=>'评论提交成功,我们会尽快审核你的回复！','code' =>1]); 	
			    }else{
			        return json(['msg'=>'评论提交成功','code' =>"1"]);
			    }			
		    }else{
			    return json(['msg'=>'评论提交失败,'.$rs->getError(),'code' =>-1]);
		    }
	    }
    }	
    public function addrecm(){	
        $param = input();	
	    if($this->request->isPost()){
			$userinfo = session('user_auth');
		    if(!$userinfo['user_id']){
		        return json(['msg'=>"请先登录",'code' =>"-2"]); 		
		    }
			if($GLOBALS['config']['user']['sys']['user_code'] == 1){
                if(!captcha_check($param['validate'])){
					return json(['msg'=>'验证码错误','code' =>-1]);
                }
            }			
            $rs=model('Cm');			
		    $cm_pid=input('post.comm_pid/d',0);
		    $cm_id=input('post.comm_id/d',0);
		    $cm_tuid=input('post.comm_tuid/d',0);
		    $cm_vid=input('post.comm_vid/d',0);
		    $cm_sid=input('post.comm_sid/d',1);
		    $data=array();
		    $data['cm_content']=htmlspecialchars(strip_tags(input('post.recomm_txt/s',''))); 
		    $data['cm_vid']=$cm_vid;
		    $data['cm_uid']=$userinfo['user_id'];
		    $data['cm_username']=(string)$userinfo['user_nickname'];
		    $data['cm_sid']=$cm_sid;
		    $data['cm_pid']=empty($cm_pid) ? $cm_id : $cm_pid;
		    $data['cm_tid']=!empty($cm_pid) ? $cm_id : 0;
		    $data['cm_tuid']=!empty($cm_tuid) ? $cm_tuid : 0;
		    $data['cm_addtime']=time();
		    $data['checktime']=1;
		    if(empty($data['cm_content'])){
			    return json(['msg'=>"内容不能为空!",'code' =>"-1"]);
		    }
		    $cookiename=md5('cm-'.bindec(decbin(ip2long(request()->ip()))).'-'.intval($data['cm_vid']).'-'.intval($data['cm_uid']));
		    $cookie=cookie($cookiename);	
	        if($cookie){
			    return json(['msg'=>'亲您评论速度太快了,请休息一会，喝杯咖啡！','code' =>-1]); 
		    }																																			
		    if($rs->validate('cm')->save($data)!== false){
			    if($GLOBALS['config']['user']['sys']['user_cm_score']){
				    model('Score')->user_score($userinfo['user_id'],11, intval($GLOBALS['config']['user']['sys']['user_cm_score'])); 
			    }
			    cookie($cookiename,true,intval($GLOBALS['config']['user']['sys']['user_cm_time']));
			    if($GLOBALS['config']['user']['sys']['user_check']==1){
			        return json(['msg'=>'评论提交成功,我们会尽快审核你的回复！','code' =>1]); 	
			    }else{
			        return json(['msg'=>'评论提交成功','code' =>"1"]);
			    }			
		    }else{	
			    return json(['msg'=>'评论提交失败,'.$rs->getError(),'code' =>-1]);
		    }
	    }	
    }
	public function logout(){
	    $jump=input('jump/d',0);
        model('user')->signout();
		if($jump){
			return json(['msg'=>'退出成功','code'=>1,'url'=>zanpian_user_url('user/login/index'),'wait'=>1]); 	
		}else{
			return json(['msg'=>'退出成功','code'=>1]);
		}	
	}	
}
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------


